﻿using System.Windows.Input;
using EmployeeTracker.ViewModel.Helpers;
using System;
using EmployeeTracker.Model;
using System.Collections.ObjectModel;
using EmployeeTracker.Gateway;

namespace EmployeeTracker.ViewModel
{
    
  
    /// <summary>
    /// ViewModel of an list of countries
    /// </summary>
    public class CountriesDetailViewModel : ViewModelBase
    {
        public  ObservableCollection<Country> AllCountries { get; set; }
        CountryGateway countryGateway = new CountryGateway();
        ///summary
       ///initialises a new instance of CountryViewModel class
       ///summary
        public CountriesDetailViewModel(ObservableCollection<Country > allCountries)
       {
           currentCountry = new Country();
           if (allCountries == null || allCountries.Count == 0)
           {
               
               AllCountries = countryGateway.GetAllcountries();
           }
           else
           {
               this.AllCountries = allCountries;
           }

           /// Re-act to any changes from outside this ViewModel
           this.AllCountries.CollectionChanged += (sender, e) =>
           {
               if (e.OldItems != null && e.OldItems.Contains(this.CurrentCountry))
               {
                   this.CurrentCountry = null;
               }
           };
           
 
           this.SaveCommand = new DelegateCommand((o) => this.Save());
           this.AddNewCommand = new DelegateCommand((o) => this.AddNew());
           this.CancelCommand = new DelegateCommand((o) => this.Cancel());
           this.CloseCommand = new DelegateCommand((o) => this.Close());
       }

       public ICommand AddNewCommand { get; private set; }
       public ICommand SaveCommand { get; private set; }
       public ICommand CancelCommand { get; private set; }
       public ICommand CloseCommand { get; private set; }

       private void Save()
       {
           if (this.CurrentCountry != null)
           {
               Country country = new Country() { CountryName = this.CurrentCountry.CountryName, CountryNationality = this.CurrentCountry.CountryNationality };
               bool flag = false;
               if (string.IsNullOrEmpty(country.CountryName))
                   return;
               foreach (Country con in this.AllCountries)
               {
                   if (con.CountryName == country.CountryName)
                   {
                       flag = true;
                       break;
                   }
               }
               if (flag)
               {
                   System.Windows.Forms.MessageBox.Show("The country details are already listed");
                   return;
               }
               this.AllCountries.Add(country);
               this.CurrentCountry = country;
               countryGateway.SaveNewCountryToDatabase(this.currentCountry.CountryName, this.currentCountry.CountryNationality, this.currentCountry.CountryEU, this.currentCountry.CountryMigration);
               System.Windows.Forms.MessageBox.Show("The country details are saved sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please fill country and nationality details");
           }
       }

       private void AddNew()
       {
           //if (this.CurrentCountry != null)
           //{
           //    Country country = new Country() { CountryName = this.CurrentCountry.CountryName, CountryNationality = this.CurrentCountry.CountryNationality };
           //    this.AllCountries.Add(country);
           //}
           //else
           //{
           //    System.Windows.Forms.MessageBox.Show("Please fill country and nationality details");
           //}
       }
       private void Cancel()
       {
           this.currentCountry = null;
       }
       private void Close()
       {
         
       }

       /// <summary>
       /// The deprtment currently selected in the workspace
       /// </summary>
       private Country  currentCountry;

       /// <summary>
       /// Gets or sets the deprtment currently selected in the workspace
       /// </summary>
       public Country CurrentCountry
       {
           get
           {
               return this.currentCountry;
           }

           set
           {
               this.currentCountry = value;
               this.OnPropertyChanged("CurrentCountry");
           }
       }       
    }
}
